import java.util.Arrays;

public class Test {
    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length - 1;
        int mid = (right + left) / 2;
        while (left <= right) {
            if (array[mid] < key) {
                left = mid + 1;
            } else if (array[mid] > key) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void bubbleSort(int[] array) {
        //趟数
        for (int i = 0; i < array.length - 1; i++) {
            //每趟交换的次数
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j+1]) {
                    swap(array,j,j+1);
                }
            }
        }
    }

    public static void main9(String[] args) {
        int[] array = {7,9,10,6,5};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }

    public static boolean consecutiveOddNums(int[] array) {
        int i = 0;
        int count = 0;
        while(i < array.length) {
            if (array[i] % 2 != 0) {
                count++;
                if (count == 3) {
                    return true;
                }
            }
            if (array[i] % 2 == 0) {
                count = 0;
            }
            i++;
        }
        return false;
    }

    public static void main8(String[] args) {
        int[] array = {1,2,34,3,4,5,7,23,12};
        //int[] array = {2,6,4,1};
        System.out.println(consecutiveOddNums(array));
    }

    public static int findMostNum(int[] array) {
        Arrays.sort(array);
        return array[array.length/2];
    }

    public static void main7(String[] args) {
        int[] array = {2,2,1,1,1,2,2};
        System.out.println(findMostNum(array));
    }

    public static int findOnceNum(int[] array) {
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret ^= array[i];
        }
        return ret;
    }

    public static void main6(String[] args) {
        int[] array = {2,1,2};
        int ret = findOnceNum(array);
        System.out.println(ret);;
    }

    public static int[] findNum(int[] nums,int target) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i,j};
                }
            }
        }
        return new int[]{-1,-1};
    }

    public static void main5(String[] args) {
        int[] nums = {2,7,11,15};
        int[] ret = findNum(nums,9);
        System.out.println(Arrays.toString(ret));
    }

    public static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void changeNum(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while(left < right) {
            while (left < right && array[left] % 2 != 0) {
                left++;
            }
            while (left < right && array[right] % 2 == 0) {
                right--;
            }
            swap(array,left,right);
        }
    }

    public static void main4(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6};
        changeNum(array);
        System.out.println(Arrays.toString(array));;
    }

    public static void transform(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }

    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5};
        transform(array);
        System.out.println(Arrays.toString(array));
    }


    //非递归
    public static void sum1(int num) {
        if (num < 10) {
            System.out.println(num);
            return;
        }
        int sum = 0;
        while (num != 0) {
            sum += (num % 10);
            num /= 10;
        }
        System.out.println(sum);
    }



    //递归打印数字的每一位
    public static void print(int num) {
        if (num < 10) {
            System.out.print(num + " ");
            return;
        }
        print(num / 10);
        System.out.print(num % 10 + " ");
    }

    public static void main2(String[] args) {
        print(1234);
    }

    public static String myToString(int[] array) {
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str = str + array[i];
            if (i != array.length - 1) {
                str += ", ";
            }
        }
        str += "]";
        return str;
    }

    public static void main1(String[] args) {
        int[] array = {1,2,3,4};
        String ret = myToString(array);
        System.out.println(ret);
    }
}